package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.NoticeModel;

/**
 * Created by Administrator on 2016/3/23.
 */
public class SearchNoticeList extends BaseActivity {
    @CodeNote(id = R.id.notice_lv)
    ListView lv;
    List<NoticeModel> list = null;
    @CodeNote(id = R.id.notice_search_toolbar)
    TToolbar title;
    @CodeNote(id = R.id.notice_list_search)
    EditText search;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_notice_list);
        //键盘暂时不出现
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        list = finalDb.findAllByWhere(NoticeModel.class," 1=1 order by NoticeCreateTime desc");
        if (getCurrentFocus() != null) {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void initEvents() {
        title.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        lv.setAdapter(new CommonAdapter<NoticeModel>(this, list, R.layout.item_notice) {
            @Override
            public void convert(ViewHolder holder, NoticeModel noticeModel, int position) {
                String title = noticeModel.getNoticeTitle();
                if (title.length() > 10) {
                    title = Utils.cutStringWithDian(title, 10);
                }
                holder.setText(R.id.notice_createtime, Utils.loadTime(noticeModel.getNoticeCreateTime()));
                holder.setText(R.id.notice_name, title);
                if (Html.fromHtml(noticeModel.getNoticeContent()).toString().length() > 10) {
                    holder.setText(R.id.notice_content, Html.fromHtml(noticeModel.getNoticeContent()).toString().substring(0, 10) + "…");
                } else {
                    holder.setText(R.id.notice_content, Html.fromHtml(noticeModel.getNoticeContent()).toString());
                }
                if (noticeModel.getIsRead() == 1) {
                    holder.setBackgroundRes(R.id.notice_isread, R.mipmap.notic_yidu);
                    holder.setTextColors(R.id.notice_name, getResources().getColor(R.color.main_item_normal));
                } else {
                    holder.setTextColors(R.id.notice_name, getResources().getColor(R.color.main_item_pressed));
                    holder.setBackgroundRes(R.id.notice_isread, R.mipmap.notic_weidu);
                }
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchNoticeList.this, ViewNoticeActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("NoticeModel", list.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
                NoticeModel noticeModel = list.get(position);
                noticeModel.setIsRead(1);
                finalDb.update(noticeModel);
                initEvents();

            }
        });
        search.addTextChangedListener(textWatcher);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            list = finalDb.findAllByWhere(NoticeModel.class, "NoticeTitle like '%" + search.getText().toString() + "%' or NoticeAuthor like '%" + search.getText().toString() + "%' order by NoticeCreateTime desc");
            initEvents();
        }
    };
}
